<template>
  <div>
    <div class="doumee-table-buttons">
      <el-button type="primary" @click="toAdd(0)">新增</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column align="center" label="系统厂商" prop="manufacturerName" min-width="140" show-overflow-tooltip />
      <el-table-column align="center" label="接口地址" prop="url" min-width="140" show-overflow-tooltip />
      <el-table-column align="center" label="登录账号" prop="account" min-width="120" show-overflow-tooltip />
      <el-table-column align="center" label="适用场馆" prop="venueName" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.venueType === '0'?'全部场馆':scope.row.venueName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="createTime" min-width="160" show-overflow-tooltip />
      <el-table-column align="center" label="状态" min-width="100" fixed="right">
        <template slot-scope="scope">
          <!-- :disabled="meta.indexOf('ComboStatus') == -1" -->
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="statusAction(scope.row.id, scope.row.status)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="200" align="center" fixed="right">
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              type="text"
              @click="toAdd('1', scope.row.id)"
            >编辑</el-button>
            <el-button
              type="text"
              class="red"
              @click="toDelete(scope.row.id)"
            >删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <!-- 新增 编辑 -->
    <el-dialog
      :close-on-click-modal="modalDialog"
      :lose-on-press-escape="modalDialog"
      :title="addEditType ? '编辑停车配置' : '新增停车配置'"
      :visible.sync="shopDia"
      width="600px"
      @closed="cancelDia"
    >
      <el-form
        ref="ruleForm"
        :model="addForm"
        label-width="6em"
        :rules="rules"
      >
        <el-form-item label="系统厂商" prop="manufacturer">
          <el-select
            v-model="addForm.manufacturer"
            placeholder="请选择系统厂商"
            style="width: 100%;"
          >
            <el-option
              v-for="item in manufacturerList"
              :key="item.id"
              :label="item.manufacturer"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="接口地址" prop="url">
          <el-input
            v-model="addForm.url"
            placeholder="请输入"
            maxlength="200"
          />
        </el-form-item>
        <el-form-item label="登录账号" prop="account">
          <el-input
            v-model="addForm.account"
            placeholder="请输入"
            maxlength="30"
          />
        </el-form-item>
        <el-form-item label="登录密码" prop="pwd">
          <el-input
            v-model="addForm.pwd"
            placeholder="请输入"
            autocomplete="new-password"
            type="password"
            maxlength="30"
          />
        </el-form-item>
        <el-form-item label="适用场馆" prop="venueType">
          <el-radio-group v-model="addForm.venueType">
            <el-radio label="0">全部场馆可用</el-radio>
            <el-radio label="1">指定场馆可用</el-radio>
          </el-radio-group>
          <div v-if="addForm.venueType == 1">
            <el-checkbox-group v-model="addForm.venueList">
              <el-checkbox
                v-for="item in relation"
                :key="item.id"
                :label="item.id"
                :name="item.id"
              >{{ item.name }}</el-checkbox>
            </el-checkbox-group>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button v-preventReClick @click="cancelDia">取消</el-button>
        <el-button
          :loading="publishLoading"
          type="primary"
          @click="confirmSub"
        >确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pdConAdd, pdConDel, pdConDisable, pdConEdit, venueList, pdConEditDetail, pdConList, manufacturerList } from '@/api'
import { parseTime } from '@/utils'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  name: 'NoticeVenue',
  components: {
    Pagination
  },
  data() {
    return {
      queryFormConfig,
      modalDialog: this.$store.state.app.dialog,
      publishLoading: false,
      listLoading: false,
      list: [],
      meta: [],
      manufacturerList: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {},
      relation: [],
      addEditType: 0,
      shopDia: false,
      addForm: {
        venueType: '0',
        venueList: []
      },
      rules: {
        account: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入登录密码', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入接口地址', trigger: 'blur' }
        ],
        manufacturer: [
          { required: true, message: '请选择系统厂商', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getVenueList()
    this.getManufacturer()
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    loadingTable() {
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },
    getManufacturer() {
      manufacturerList({ param: { type: 9 }})
        .then((res) => {
          if (res.errorCode === '000000') {
            this.manufacturerList = res.recordList || []
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    statusAction(id, status) {
      pdConDisable({ param: { id, status }}).then(res => {
        this.getList()
        if (res.errorCode === '000000') {
          this.$message.success(status === 0 || status === '0' ? '禁用成功' : '启用成功')
        }
      }, () => {
        this.getList()
      })
    },
    // 获取场馆列表
    getVenueList() {
      venueList({
        param: {}
      }).then(res => {
        if (res.errorCode !== '000000') return
        this.relation = res.recordList
        const arr = res.recordList
        this.queryFormConfig.formItems[0].options = arr
      })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      this.listLoading = true
      pdConList({ pagination, param }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
          if (res.totalCount && res.recordList.length === 0 && pagination.page > 1) {
            this.getList(Math.ceil(res.totalCount / pagination.rows))
          }
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    },
    // 新增/编辑消息
    toAdd(type, id) {
      this.addEditType = type
      this.shopDia = true
      if (id) {
        pdConEditDetail({ param: { id }}).then(res => {
          if (res.errorCode === '000000') {
            const obj = res.record
            obj.venueList = obj.venueIds.split(',')
            this.addForm = obj
          }
        })
      } else {
        this.addForm = {
          venueType: '0',
          venueList: []
        }
      }
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate()
      })
    },
    // 取消新增/编辑
    cancelDia() {
      this.shopDia = false
    },
    // 确认新增/编辑
    confirmSub() {
      const that = this
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const { addEditType } = this
          const param = { ...this.addForm }
          if (param.venueType && param.venueType === '1') {
            if (!param.venueList || param.venueList.length === 0) {
              return this.$message.warning('请选择指定场馆')
            }
          }
          param.venueIds = param.venueList.join(',')
          that.publishLoading = true
          if (addEditType) {
            pdConEdit({ param }).then(res => {
              if (res.errorCode !== '000000') return
              that.publishLoading = false
              that.shopDia = false
              that.getList()
            }).catch((er) => {
              that.publishLoading = false
              console.log(er)
            })
          } else {
            pdConAdd({ param }).then(res => {
              if (res.errorCode !== '000000') return
              that.publishLoading = false
              that.shopDia = false
              that.getList()
            }).catch((er) => {
              that.publishLoading = false
              console.log(er)
            })
          }
        } else {
          that.publishLoading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    // 删除
    toDelete(id) {
      this.$confirm('此操作将删除该设置, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          pdConDel({ param: { id }}).then(ress => {
            if (ress.errorCode === '000000') {
              this.getList()
            }
          })
        })
        .catch(() => {
          // this.$message.info('已取消删除')
        })
    }
  }
}
</script>

